/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.view;

import ru.ssau.lib.GBC;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

/**
 *
 * @author Дима
 */
public class ThreadDialog extends JDialog {

    private JTextField captionField;

    private JButton okButton;
    private JButton cancelButton;

    private ru.ssau.entities.Thread thread;

    public ThreadDialog(Frame parent) {
        super(parent, "Add/edit thread", true);
        initComponents();
        layoutComponents();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                thread = null;
                setVisible(false);
            }
        });

        pack();
        setMinimumSize(getPreferredSize());
    }

    private void initComponents() {
        captionField = new JTextField(10);

        okButton = new JButton("OK");
        okButton.addActionListener(new OkAction());
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new CancelAction());

        
    }

    private void layoutComponents() {
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        JPanel threadPanel = new JPanel(new GridBagLayout());
        add(threadPanel);
        Border border = BorderFactory.createEtchedBorder();
        threadPanel.setBorder(BorderFactory.createTitledBorder(border, "Thread"));

        JLabel caption = new JLabel("Caption:", JLabel.RIGHT);
        threadPanel.add(caption, new GBC(0,0).setInsets(0,0,5,5));
        threadPanel.add(captionField, new GBC(1,0).setInsets(0,0,0,5).setWeight(100,0));

        JPanel buttonPanel = new JPanel();
        add(buttonPanel);
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
    }

    public ru.ssau.entities.Thread showDialog(ru.ssau.entities.Thread aThread) {
        thread = aThread;
        if(thread == null) {
            thread = new ru.ssau.entities.Thread();
        }
        captionField.setText(thread.getCaption());

        captionField.grabFocus();
        setVisible(true);
        return thread;
    }

    private class OkAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //try {
                /*if(captionField.getText().trim().equals(""))
                    throw new InvalidFieldValueException("Caption field is empty");*/
                thread.setCaption(captionField.getText());
                setVisible(false);
            //}
            /*catch(InvalidFieldValueException exception) {
                JOptionPane.showMessageDialog(ThreadDialog.this, exception.getMessage(), "Input error", JOptionPane.ERROR_MESSAGE);
            }*/
        }
    }

    private class CancelAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            thread = null;
            setVisible(false);
        }
    }
}
